package ca.tatham.triplipse.views.tree;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.DrillDownAdapter;

import ca.tatham.triplipse.RdfStatement;
import ca.tatham.triplipse.views.IRdfStatementLabelProvider;
import ca.tatham.triplipse.views.RdfDocumentContentProvider;
import ca.tatham.triplipse.views.RdfDocumentView;

/**
 * The view is connected to the model using a content provider.
 * <p>
 * The view uses a label provider to define how model objects should be
 * presented in the view. Each view can present the same model objects using
 * different labels and icons, if needed. Alternatively, a single label provider
 * can be shared between views in order to ensure that objects of the same type
 * are presented in the same way everywhere.
 * <p>
 */

public class TreeRdfDocumentView extends RdfDocumentView {

	public static final String VIEW_ID = "ca.tatham.triplipse.views.TreeRdfDocumentView";//$NON-NLS-1$

	private DrillDownAdapter m_drillDownAdapter;

	public TreeRdfDocumentView() {
		// do nothing
	}

	@Override
	protected StructuredViewer createViewer(Composite parent) {
		return new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
	}

	@Override
	protected ViewerSorter createSorter() {
		return new ViewerSorter();
	}

	@Override
	protected RdfDocumentContentProvider createContentProvider() {
		return new TreeRdfDocumentContentProvider(getViewer(), getViewSite());
	}

	@Override
	protected IRdfStatementLabelProvider createLabelProvider() {
		return new TreeRdfStatementDecoratingLabelProvider(getViewer());
	}

	@Override
	protected void fillContextMenuAdditional(IMenuManager manager) {
		manager.add(new Separator());
		getDrillDownAdapter().addNavigationActions(manager);
	}

	@Override
	protected void fillLocalToolBarAdditional(IToolBarManager manager) {
		getDrillDownAdapter().addNavigationActions(manager);
	}

	@Override
	protected void fillLocalPullDownAdditional(@SuppressWarnings("unused")
	IMenuManager menu) {
		// do nothing
	}

	private DrillDownAdapter getDrillDownAdapter() {
		if (m_drillDownAdapter == null) {
			m_drillDownAdapter = new DrillDownAdapter((TreeViewer) getViewer());
		}
		return m_drillDownAdapter;
	}

	protected RdfStatement getStatementFromSelection(Object selectedObject) {
		RdfStatement statement = ((TreeRdfDocumentContentProvider) getContentProvider())
				.getTriple((TreeObject) selectedObject);
		return statement;
	}

}